# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

# 默认执行安卓平台
default_platform(:android)
# 打包平台-安卓 fastlane android 内部lane定义的指令
platform :android do
  project_dir = 'android/'
  # 该打包脚本命令的描述
  desc 'Android: 执行打包过程'
  # beta是打包的脚本命令 打包上传时执行 fastlane build
  private_lane :build do |options|
    sh('flutter clean')
    # 清空指定目录参数
    gradle(task: 'clean', project_dir: project_dir)
    sh('flutter pub get')
    gradle(
      # 构建任务，支持执行多个任务，以数组传入[]
      task: 'assemble', # assemble（常规），bundle（AAB后缀），clean（清空构建目录） 
      build_type: options[:type], # 打包类型，Release
      project_dir: project_dir, # gradle项目的根目录
    )
  end

  after_all do |lane|
    say("Successfully finished deployment (#{lane})!")
  end

  desc 'Android: 打包上传蒲公英'
  lane :pgy do |options|
    apkType = options[:type] || 'Release'
    apkUrl = "#{File.expand_path("..", Dir.pwd)}/build/app/outputs/apk/#{apkType.downcase}/app-#{apkType.downcase}.apk"
    build(type: apkType)
    # 上传到蒲公英，api_key和user_key在蒲公英网站获取
    pgyer(
      api_key: "a123456789", 
      user_key: "a123456789",
      apk: apkUrl,
      update_description: options[:desc])
  end

  # 自定义lane命令，fastlane android buildApk相当于执行此代码
  desc 'Android本地打包'
  lane :buildApk do |options|
    apkType = options[:type] || 'Release'
    build(type: apkType)
  end
end

